package com.xyw.code.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.xyw.code.constant.CommonStatusEnum;
import com.xyw.code.entity.SysDept;
import com.xyw.code.log.SysLog;
import com.xyw.code.service.ISysDeptService;
import com.xyw.code.utils.Result;
import io.swagger.annotations.Api;
import org.aspectj.weaver.tools.CommonsTrace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author xuyiwei
 * @since 2019-07-30
 */
@Api(value="部门模块")
@RestController
@RequestMapping("/dept")
public class SysDeptController {

    @Autowired
    private ISysDeptService deptService;

    /**
     * 得到部门的列表
     *
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:dept:view')")
    public Result<List<SysDept>> getDeptList() {
        return Result.ok(deptService.selectDeptList());
    }

    /**
     * 新增部门
     * @return
     */
    @SysLog(descrption = "新增部门")
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:dept:add')")
    public Result<Boolean> add(@RequestBody SysDept dept){
        return Result.ok(deptService.add(dept));
    }

    /**
     * 更新部门
     * @return
     */
    @SysLog(descrption = "更新部门")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('sys:dept:update')")
    public Result<Boolean> update(@RequestBody SysDept dept){
        dept.setUpdateTime(new Date());
        return Result.ok(deptService.updateById(dept));
    }

    /**
     * 删除部门
     * @return
     */
    @SysLog(descrption = "删除部门")
    @PostMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:dept:delete')")
    public Result<Boolean> delete(@PathVariable Integer id){
        SysDept dept = new SysDept();
        dept.setId(id);
        dept.setStatus(CommonStatusEnum.DELETE.getType());
        dept.setUpdateTime(new Date());
        return Result.ok(deptService.updateById(dept));
    }

    /**
     * 获得部门下拉框树
     * @return
     */
    @GetMapping("/tree")
    public Result getDeptTree() {
        return Result.ok(deptService.getDeptTree());
    }

}

